$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tblschool/list',
        datatype: "json",
        colModel: [
            {label: 'ID', name: 'schoolId', index: 'school_id', width: 50, key: true},
            {label: '校区名称', name: 'schoolName', index: 'school_name', width: 80},
            {label: '校区代码', name: 'schoolCode', index: 'school_code', width: 80},
            {label: '主体', name: 'organization', index: 'organization', width: 80},
            {label: '教师组长TRS', name: 'trsId', index: 'trs_id', width: 80, hidden: true},
            {label: '教师组长', name: 'username', index: 'username', width: 80},
            {label: '校长', name: 'principalName', index: 'principalName', width: 80},
            {label: '公司', name: 'companyName', index: 'companyName', width: 80},
            {label: '地址', name: 'address', index: 'address', width: 80},
            {label: '区域', name: 'area', index: 'area', width: 80},
            /*{ label: '大区', name: 'bigAreaId', index: 'big_area_id', width: 80 },*/
            {label: '联系电话', name: 'concatorPhone', index: 'concator_phone', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80},

            {label: '备注', name: 'attribute2', index: 'attribute2', width: 80, hidden: true},
            {label: '备注', name: 'attribute3', index: 'attribute3', width: 80, hidden: true},
            {label: '备注', name: 'attribute4', index: 'attribute4', width: 80, hidden: true},
            {label: '备注', name: 'attribute5', index: 'attribute5', width: 80, hidden: true}

            /*{ label: '', name: 'createTime', index: 'create_time', width: 80 },
             { label: '', name: 'createBy', index: 'create_by', width: 80 },
             { label: '', name: 'updateTime', index: 'update_time', width: 80 },
             { label: '', name: 'updateBy', index: 'update_by', width: 80 }*/
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});

        }
    });

});

var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};

var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            schoolName: null
        },
        showList: true,
        title: null,
        tblSchool: {
            deptId: null,
            deptName: null
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            $.get(baseURL + "sys/dept/list", function (r) {
                ztree = $.fn.zTree.init($("#deptTree"), setting, r);
                var node = ztree.getNodeByParam("deptId", vm.tblSchool.deptId);
                if (node != null) {
                    ztree.selectNode(node);

                    vm.tblSchool.deptName = node.name;
                }
            })
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.tblSchool = {deptName: null, deptId: null, status: 1};
            vm.school = {};
            vm.getDept();
        },
        update: function (event) {
            vm.getDept();
            var schoolId = getSelectedRow();
            if (schoolId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            $.get(baseURL + "sys/tblschool/info/"+schoolId, function(r){
                vm.tblSchool = r.tblSchool;

            });
        },
        saveOrUpdate: function (event) {
            var url = vm.tblSchool.schoolId == null ? "sys/tblschool/save" : "sys/tblschool/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.tblSchool),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var schoolIds = getSelectedRows();
            if (schoolIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/tblschool/delete",
                    contentType: "application/json",
                    data: JSON.stringify(schoolIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                                /*$("#jqGrid").trigger("reloadGrid");*/
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        deptTree: function () {
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#schoolLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.tblSchool.deptId = node[0].deptId;
                    vm.tblSchool.deptName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'schoolName': vm.q.schoolName},
                page: page
            }).trigger("reloadGrid");
        }
    }
});



